package com.inscream.app.profileTab;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import com.inscream.app.HomeActivity;
import com.inscream.app.JSONParser;
import com.inscream.app.R;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;

public class MyFollowers extends Activity {

    ListView myFollowerList;
    private static String tturl = "http://mehmetalp.com.tr/seslitwitter/homepage.php?method=5";
    private static final String TAG_OS = "posts";
    private static final String soundRecordTag = "soundRecordTag";
    JSONArray android = null;
    ArrayList<HashMap<String, String>> followerList = new ArrayList<HashMap<String, String>>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_followers);
        new getMyFollower().execute();
    }


    private class getMyFollower extends AsyncTask<String, String, JSONObject> {
        private ProgressDialog pDialog;
        @Override
        protected void onPreExecute() {
            super.onPreExecute();

            pDialog = new ProgressDialog(MyFollowers.this);
            pDialog.setMessage("Wait a min ...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(true);
            pDialog.show();
        }

        @Override
        protected JSONObject doInBackground(String... args) {
            JSONParser jParser = new JSONParser();
            // Getting JSON from URL
            JSONObject ttjson = jParser.getJSONFromUrl(tturl);
            return ttjson;
        }

        @Override
        protected void onPostExecute(JSONObject json) {
            pDialog.dismiss();
            try {
                // Getting JSON Array from URL
                android = json.getJSONArray(TAG_OS);
                for(int i = 0; i < android.length(); i++){
                    JSONObject c = android.getJSONObject(i);
                    // Storing  JSON item in a Variable
                    String getTTListName = c.getString(soundRecordTag);

                    // Adding value HashMap key => value
                    HashMap<String, String> ttmap = new HashMap<String, String>();
                    ttmap.put(soundRecordTag, getTTListName);

                    followerList.add(ttmap);
                    myFollowerList =(ListView)findViewById(R.id.myFollowerList);
                    ListAdapter myNewAdapter = new SimpleAdapter(MyFollowers.this, followerList,
                            R.layout.myfollower,
                            new String[]{soundRecordTag}, new int[]{R.id.followerUsername});
                    myFollowerList.setAdapter(myNewAdapter);
                    myFollowerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView<?> parent, View view,
                                                int position, long id) {
                            Toast.makeText(MyFollowers.this, "You Clicked at " + followerList.get(+position).get("soundRecordTag"), Toast.LENGTH_SHORT).show();
                            String getThisTTItem = followerList.get(+position).get("soundRecordTag").toString();
                            Toast.makeText(MyFollowers.this, "You Clicked at " + getThisTTItem, Toast.LENGTH_SHORT).show();
                            Intent a = new Intent(MyFollowers.this, HomeActivity.class);
                            a.putExtra("getThisTTItem", getThisTTItem);
                            startActivity(a);
                        }
                    });
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }



    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.my_followers, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

}
